# encoding=utf-8
import time
#冒泡排序 对列表进行升序排序

# def bubble_sort(alist):
#     '''
#     1 相邻两个元素进行比较，如果发现位置错误，则进行交换
#     2 '''
#
#     n=len(alist)
#     for k in range(n-1):
#         for i in range(n-1-k):
#             if alist[i] > alist[i+1]:
#                 alist[i],alist[i+1] = alist[i+1],alist[i]
#
#
#
# alist=[7,5,3,6,44,22,99,11]
# # print("原数组:",alist)
# bubble_sort(alist)
# print("排序后数组：",alist)
#
# def maopao_sort(alist):
#     for j in range(len(alist)-1,0,-1):
#         for i in range(j):
#             if alist[i] > alist[i+1]:
#                 alist[i], alist[i+ 1] = alist[i + 1], alist[i]
#
# plist=[99,88,77,66,55,44,33,22,11]
# maopao_sort(plist)
# print(plist)

#如果传入的列表就是有序的，则只需要检查一轮，查看是否进行交换，如果没有进行交换，说明列表就是有序的，则退出循环
def bubble_sort2(alist):
    n=len(alist)
    start_time = time.time()
    for k in range(n-1):
        count = 0
        for i in range(n-1-k):
            if alist[i] > alist[i+1]:
                alist[i],alist[i+1] = alist[i+1],alist[i]
                count+=1
        #判断count=0？说明没有交换：null
        if count==0:
            break
    end_time=time.time()
    print("冒泡排序执行时间：",end_time-start_time)
alist3 = [7,5,3,6,44,22,99,11]
alist2 = [3, 5, 6, 7, 11, 3,22, 44, 99]
bubble_sort2(alist3)
print("2:",alist3)